<?php

declare(strict_types=1);

namespace App\Entity;

use App\Models\Order;
use App\Models\OrderChannel;
use Illuminate\Support\Facades\Log;

class OrderChannelEntity extends ABaseEntity
{
    /**
     * OrderChannelEntity constructor.
     */
    public function __construct(OrderChannel $model)
    {
        $this->model = $model;
    }

    public function getId(): int
    {
        return (int) $this->model->id;
    }

    public function setId(int $id): void
    {
        $this->model->id = $id;
    }

    public function getOutid(): string
    {
        return (string) $this->model->outid;
    }

    public function setOutid(string $outid): void
    {
        $this->model->outid = $outid;
    }

    public function getChannelid(): int
    {
        return (int) $this->model->channelid;
    }

    public function setChannelid(int $channelid): void
    {
        $this->model->channelid = $channelid;
    }

    public function getCreateat(): string
    {
        return (string) $this->model->createat;
    }

    public function setCreateat(string $createat): void
    {
        $this->model->createat = $createat;
    }

    public function getUpdateat(): string
    {
        return (string) $this->model->updateat;
    }

    public function setUpdateat(string $updateat): void
    {
        $this->model->updateat = $updateat;
    }

    public function getUnit(): string
    {
        return (string) $this->model->unit;
    }

    public function setUnit(string $unit): void
    {
        $this->model->unit = $unit;
    }

    public function getNum(): int
    {
        return (int) $this->model->num;
    }

    public function setNum(int $num): void
    {
        $this->model->num = $num;
    }

    public function getPayment(): float
    {
        return (float) $this->model->payment;
    }

    public function setPayment(float $payment): void
    {
        $this->model->payment = $payment;
    }

    public function getPrice(): float
    {
        return (float) $this->model->price;
    }

    public function setPrice(float $price): void
    {
        $this->model->price = $price;
    }

    /**
     * 获取订单
     */
    public function getOrder(): ?OrderEntity
    {
        $o = Order::where('order_channelid', $this->getId())->first();
        if ($o === null) {
            return null;
        }

        return new OrderEntity($o);
    }

    public function getOutskuid(): string
    {
        return (string) $this->model->outskuid;
    }

    public function setOutskuid(string $outskuid): void
    {
        $this->model->outskuid = $outskuid;
    }

    public function getOutskuvalue(): string
    {
        return (string) $this->model->outskuvalue;
    }

    public function setOutskuvalue(string $outskuvalue): void
    {
        $this->model->outskuvalue = $outskuvalue;
    }

    public function getOutProductId(): string
    {
        return (string) $this->model->out_product_id;
    }

    public function setOutProductId(string $out_product_id): void
    {
        $this->model->out_product_id = $out_product_id;
    }

    /**
     * 根据三方id获取渠道信息
     *
     * @return static|null
     */
    public static function findOrderChannelEntityByOutid(string $outid): ?self
    {
        $order_channel = OrderChannel::where('outid', $outid)->first();
        if (empty($order_channel)) {
            Log::error(sprintf('第三方outid:%s不存在', $outid));

            return null;
        }

        return new self($order_channel);
    }
}
